package com.geeguo.ebuilder.client.base.tenant.consumer;

import com.geeguo.ebuilder.client.base.core.constants.MQConsumerGroupConstants;
import com.geeguo.ebuilder.client.base.core.constants.MQTopicConstants;
import com.geeguo.ebuilder.client.base.tenant.provider.TenantProvider;
import com.geeguo.ebuilder.core.common.utils.worker.WorkerIdHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RocketMQMessageListener(topic = MQTopicConstants.TENANT_INFO_CHANGE, consumerGroup = MQConsumerGroupConstants.CLIENT_TENANT_INFO_CHANGE, messageModel = MessageModel.BROADCASTING)
public class TenantInfoChangeMQConsumer implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.setInstanceName(String.valueOf(WorkerIdHelper.getWorkerId()));
    }

    @Override
    public void onMessage(String code) {
        log.info("收到租户信息变更消息：" + code);
        TenantProvider.clearByCode(code);
    }
}
